﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Schedulingclasses.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MyDbContext ctx;

        public BaseRepository(MyDbContext ctx)
        {
            this.ctx = ctx;
        }
        public async Task<int> AddAync(T t)
        {
            await ctx.Set<T>().AddAsync(t);
            return await ctx.SaveChangesAsync();
        }

        public IQueryable<T> Getlist()
        {
            return ctx.Set<T>().AsQueryable();
        }

        public async Task<T> GetValue(Expression<Func<T, bool>> exp)
        {
            return await ctx.Set<T>().Where(exp).FirstOrDefaultAsync();
        }

        public Task<int> Update(T t)
        {
            ctx.Set<T>().Update(t);
            return ctx.SaveChangesAsync();
        }
    }
}
